<?php
// 微信接口
header("Content-type: text/html; charset=utf-8");
require_once ("log.php");
error_reporting(E_ERROR);
require_once ("cloud_api.php");

function coll_decode($data) {
	return json_decode(urldecode($data), true);
}

class wx_login {
	// 微信登录接口
	function login() {
		$bot_id = $_COOKIE['bot_id'];
		// 操作过时
		if (empty($bot_id)) {
			echo json_encode(array(
				'state' => -1,
				'msg' 	=> '登录超时！',
			));
			exit;
		}

		// 获取操作系统的ip
		$row = simQuery("SELECT * FROM robot AS rot, server AS ser WHERE rot.server_id = ser.id AND rot.id = {$bot_id}");
		
		if($row) {
			$host = $row['host'];
			$port = $row['port'];
			$data = array();
			$data['fun'] 	= 'login_oper';
			$data['id'] 	= $bot_id;
			$data['sign'] 	= generateSign($data);
			$rt = json_decode(sendZSHttp($host, $port, '/api/api_accept_post.php', $data, 'post'), true);
			$rt['host'] = $host;
			$rt['port'] = $port;
			if ($rt['state'] < 0) {
				$rt['state'] -= 1;
			}
			echo json_encode($rt); // 操作成功
			exit;
		}

		$rt = array (
			'state' => -2,
			'msg'	=> '获取机器人数据失败!',
		);
		echo json_encode($rt); // 数据有误
	}

	// 获取微信登录码状态
	function get_login_state() {
		$bot_id = $_COOKIE['bot_id'];
		if (empty($bot_id)) {
			echo json_encode(array (
				'state' => -2,
				'msg'	=> '没有当前机器人信息!',
			));
			exit;
		}

		$host = $_GET['host'];
		$port = $_GET['port'];
		$data = array();
		$data['fun'] 	= 'get_login_state';
		$data['id']		= $bot_id;
		$data['sign'] 	= generateSign($data);
		$rt = json_decode(sendZSHttp($host, $port, '/api/api_accept_post.php', $data, 'post'), true);
		if (empty($rt)) {
			$rt['state'] = -2;
		}
		if ($rt['state'] < 0) {
			$rt['state'] -= 2;
		}
		echo json_encode($rt);
	}

	// 退出微信
	function exit() {
		$bot_id = $_COOKIE['bot_id'];

		// 操作过时
		if (empty($bot_id)) {
			echo json_encode(array (
				'state' => -1,
				'msg' 	=> '登录超时!',
			));
			exit;
		}
		$row = simQuery("SELECT * FROM robot AS rot, server AS ser WHERE rot.server_id = ser.id AND rot.id = {$bot_id}");
		if (empty($row)) {
			echo json_encode(array (
				'state' => -2,
				'msg' 	=> '没有该服务器信息!',
			));
			exit;
		}

		if ($row) {
			$host = $row['host'];
			$port = $row['port'];
			$data = array();
			$data['fun'] 	= 'exit_oper';
			$data['id']		= $bot_id;
			$data['sign'] 	= generateSign($data);
			$rt = coll_decode(sendZSHttp($host, $port,'/api/api_accept_post.php', $data, 'post'));
			if ($rt['state'] < 0) {
				$rt['state'] -= 2;
			}
			echo json_encode($rt);
			exit;
		}

		$rt = array (
			'state' => -2,
			'msg'	=> '获取机器人数据失败!',
		);
		echo json_encode($rt); // 数据有误
	}

	// 获取微信的数据(头像, 名字)
	function get_wx_data() {
		$bot_id = $_COOKIE['bot_id'];
		if (empty($bot_id)) {
			echo json_encode(array (
				'state' => -1,
				'msg'	=> '登录超时!',
			));
			exit;
		}

		$row = simQuery("SELECT * FROM robot WHERE id = {$bot_id}");
		if (empty($row)){
			echo json_encode(array (
				'state' => -2,
				'msg'	=> '没有当前机器人信息!',
			));
			exit;
		}
		if (empty($row['wechat_head_url']) || empty($row['wechat_head_url'])) {
			echo json_encode(array (
				'state' => 0,
				'msg'	=> '成功!',
			));
			exit;
		} else {
			echo json_encode(array (
				'state' 	=> 1,
				'msg'		=> '成功!',
				'nick_name'	=> $row['wechat_nick_name'],
				'head_url' 	=> $row['wechat_head_url'],
			));
			exit;
		}
	}

	// 获取心跳包
	function get_heart_beat() {
		$bot_id = $_COOKIE['bot_id'];
		if (empty($bot_id)) {
			echo json_encode(array (
				'state' => -1,
				'msg'	=> '登录超时!',
			));
			exit;
		}

		$row = simQuery("SELECT * FROM robot AS rot, server AS ser WHERE rot.server_id = ser.id AND rot.id = {$bot_id}");
		if (empty($row)) {
			echo json_encode(array (
				'state' => -2,
				'msg' 	=> '没有该服务器信息!',
			));
			exit;
		}
	
		$host = $row['host'];
		$port = $row['port'];
		$data = array();
		$data['fun'] 	= 'get_heart_beat';
		$data['id']		= $bot_id;
		$data['sign'] 	= generateSign($data);
		$rt = json_decode(sendZSHttp($host, $port, '/api/api_accept_post.php', $data, 'post'), true);
		LOGINFO($rt, '=== $rt');
		if ($rt['state'] == -3) {
			$rt['state'] = 0;
		}
		if ($rt['state'] < 0) {
			$rt['state'] -= 3;
		}

		echo json_encode($rt);
	}
}